class Check {
    /**
     * 检查会员卡，次卡
     * @api {GET}         /                       检查会员卡，次卡
     * @apiName           /Check
     * @apiGroup          Check
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {}
       }
     */
    async index(ctx) {
        const { db } = ctx;
        const nowTime = Math.round(new Date() / 1000);
        // 查询所有用户
        let user = await db.User.findAll();
        user = ctx.jsonArr(user);
        for (let i = 0; i < user.length; i++) {
            // 次卡判断
            const buyCardTime = user[i].buyCardTime;
            const endCardTime = user[i].endCardTime;
            const carDay = endCardTime - buyCardTime;
            const Day = Math.round(nowTime - buyCardTime);
            console.log('次卡购买时间', buyCardTime);
            console.log('次卡时间', carDay);
            console.log('timeout', Day);

            if (Day > carDay) {
                let par = {
                    limit: 0,
                    buyCardTime: null,
                    endCardTime: 0,
                    cardTimeId: 0
                };
                await db.User.update(par, { where: { id: user[i].id } });
            }
            // 会员卡判断

            const buyMemberTime = user[i].buyMemberTime;
            const endMemberTime = user[i].endMemberTime;
            const days = endMemberTime - buyMemberTime;
            // 更新次卡会员剩余天数,当前时间减去购买时间
            const changeDay = Math.round(nowTime - buyMemberTime);
            const remaining = days - changeDay;
            let pars = {};

            console.log('会员卡购买时间', buyMemberTime);
            console.log('会员卡已过时间', changeDay);
            console.log('remaining', remaining);
            console.log('timeout', days);
            if (changeDay > days) {
                pars = {
                    leftDay: 0,
                    memberId: 0,
                    endMemberTime: null,
                    buyMemberTime: null
                };
                await db.User.update(pars, { where: { id: user[i].id } });
            } else {
                pars = {
                    leftDay: remaining
                };
                await db.User.update(pars, { where: { id: user[i].id } });
            }
        }
        ctx.success();
    }
}
module.exports = new Check();
